<?php
include_once "models/MySqlDataAccessHelper.php";
include_once "objs/MenuPojo.php";
include_once "objs/MenusPojo.php";
include_once "objs/ArticlePojo.php";

class MenuDetailDao {

    public static function getMenu($helper) {
        $ds = array();
        $sql = "select idMenu, mn.name as name from menudetail md, menu mn where idSub1=0 and idSub2=0 and md.idMenu=mn.id";
        
        //$helper = new MySqlDataAccessHelper;

        //$helper->open();
        $rs = $helper->executeQuery($sql);
        if (FALSE === $rs) {
            echo mysql_error();
            return $ds;
        } else {
            while ($row = mysql_fetch_array($rs)) {
                $menu = new MenuPojo($row["idMenu"],$row["name"]);
                array_push($ds, $menu);
            }
            //$helper->close();
            return $ds;
        }
    }
    
    public static function getMenuSub1($helper, $idMenu) {
        $ds = array();
        $sql = sprintf("select idSub1, mn.name as name from menudetail md, menu mn where idSub2=0 and md.idSub1=mn.id and idMenu=%d", $idMenu);
        
        //$helper = new MySqlDataAccessHelper;

        //$helper->open();
        $rs = $helper->executeQuery($sql);
        if (FALSE === $rs) {
            echo mysql_error();
            return $ds;
        } else {
            while ($row = mysql_fetch_array($rs)) {
                $menu = new MenuPojo($row["idSub1"],$row["name"]);
                array_push($ds, $menu);
            }
            //$helper->close();
            return $ds;
        }
    }
    
    public static function getMenuSub2($helper, $idMenu, $idSub1) {
        $ds = array();
        $sql = sprintf("select idSub2, mn.name as name from menudetail md, menu mn where idMenu=%d and idSub1=%d and md.idSub2=mn.id", $idMenu, $idSub1);
        
        //$helper = new MySqlDataAccessHelper;

        //$helper->open();
        $rs = $helper->executeQuery($sql);
        if (FALSE === $rs) {
            echo mysql_error();
            return $ds;
        } else {
            while ($row = mysql_fetch_array($rs)) {
                $menu = new MenuPojo($row["idSub2"],$row["name"]);
                array_push($ds, $menu);
            }
            //$helper->close();
            return $ds;
        }
    }
    
    public static function getAllMenus() { 
        $helper = new MySqlDataAccessHelper;

        //$helper->open();
        $menuItems = MenuDetailDao::getMenu($helper);
        $ds = array();
        foreach($menuItems as $item){
           // echo $item->getName()."<br/>";
            $subMenus = MenuDetailDao::getMenuSub1($helper, $item->getId());
            $ds1 = array();
            foreach($subMenus as $item1){
                //echo "***".$item1->getName()."<br/>";
                $subMenu1 = MenuDetailDao::getMenuSub2($helper, $item->getId(), $item1->getId());
                $ds2 = array();
                foreach ($subMenu1 as $item2) {
                  //  echo "******".$item2->getName()."<br/>";
                    $menus2 = new MenusPojo($item2, array());
                    array_push($ds2, $menus2);
                }
                $menus1 = new MenusPojo($item1, $ds2);
                array_push($ds1, $menus1);
            }
            $menus = new MenusPojo($item, $ds1);
            array_push($ds, $menus);
        }
        //$helper->open();
        return $ds;        
    }
    
    public static function getArticles($idMenu, $idSub1, $idSub2) {
        $ds = array();
        $sql = sprintf("select a.id as id, a.name as name, a.idSubject as idSubject, a.content as content from menudetail md, articles a where idMenu=%d and idSub1=%d and idSub2=%d and md.idSubject=a.idSubject", $idMenu, $idSub1, $idSub2);
        
        $helper = new MySqlDataAccessHelper;

        $helper->open();
        $rs = $helper->executeQuery($sql);
        if (FALSE === $rs) {
            echo mysql_error();
            return $ds;
        } else {
            while ($row = mysql_fetch_array($rs)) {
                $article = new ArticlePojo($row["id"],$row["name"],$row["idSubject"],$row["content"]);
                array_push($ds, $article);
            }
            $helper->close();
            return $ds;
        }
    }
    

    public static function insertMenuDetail($idMenu, $idSub1, $idSub2) {
        $idSubject = $idMenu*100 + $idSub1*10 + $idSub2;
        $sql = sprintf("INSERT INTO menudetail (idSubject, idMenu, idSub1, idSub2) Values(%d, %d, %d, %d)", $idSubject, $idMenu, $idSub1,  $idSub2);
        $helper = new MySqlDataAccessHelper;

        $helper->open();
        $helper->executeUpdate($sql);
        $helper->close();
    }
}

?>